using Zhaoxi.Zhihu.Core.Common;
using Zhaoxi.Zhihu.SharedKerel.Result;

namespace Zhaoxi.Zhihu.Core.AppUserAggregate.Entites;

public class FollowUser:BaseEntity
{
    private List<FollowUser>  _followees;
    private List<FollowUser> _followers;
    private List<FollowQuestion> _followQuestions;

    //关注着
    public int FollowerId { get; set; }
    public AppUser Follower { get; set; } = null!;
    
    //被关注着
    public int FolloweeId { get; set; }
    public AppUser Followee { get; set; } = null!;
    
    public DateTimeOffset FollowDate { get; set; }


    public IReadOnlyCollection<FollowUser> Followees =>_followees.AsReadOnly();
    public IReadOnlyCollection<FollowUser> Followers =>_followers.AsReadOnly();
    public IReadOnlyCollection<FollowQuestion> FollowQuestions=> _followQuestions.AsReadOnly();
    
    public Result AddFollowee(int followeeId)
    {
        if (_followees.Any(fu => fu.FolloweeId == followeeId))
        {

            return Result.Invalid("该用户已关注");
        }

        var followUser = new FollowUser
        {
            FollowerId = Id,
            FolloweeId = followeeId,
            FollowDate = DateTimeOffset.Now
        };
        _followees. Add(followUser);
        return Result.Success();
    }


    public void RemoveFollowee(int followeeId)
    {
        var followUser = _followees.FirstOrDefault(fu=> fu.FolloweeId == followeeId);
        if (followUser == null) return;
        _followees. Remove(followUser);
    }
}